找传奇、传世资源到传世资源站!

自定义dialog实现日期选择器

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

自定义dialog实现日期选择器 Android平台开发-第1张
public class DateDialog extends Dialog implements WheelView.OnSelectListener{ private TextView tv_date_cancel, tv_date_sure; private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器 private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器 Context mContext; TextView tv_title; String title,date; CallBackListener mCallBackListener; /** * 获取选择的年 * * @return */ public String getYear() { Log.d("TAG", mWheelYear.getSelectedText()); return mWheelYear.getSelectedText(); } /** * 获取选择的月 * * @return */ public String getMonth() { Log.d("TAG", mWheelMonth.getSelectedText()); return mWheelMonth.getSelectedText(); } /** * 获取选择的日 * * @return */ public String getDay() { Log.d("TAG", mWheelDay.getSelectedText()); return mWheelDay.getSelectedText(); } public String getDate() { Log.d("TAG", mWheelYear.getSelectedText() "-" mWheelMonth.getSelectedText() "-" mWheelDay.getSelectedText()); date = mWheelYear.getSelectedText() "-" mWheelMonth.getSelectedText() "-" mWheelDay.getSelectedText(); return date; } private WheelView mWheelYear; private WheelView mWheelMonth; private WheelView mWheelDay; public DateDialog(Context context, String str) { super(context); mContext = context; title = str; } public DateDialog(Context context, int themeResId) { super(context, R.style.MyDialog); } /** * 设置取消按钮的显示内容和监听 * * @param onNoOnclickListener */ public void setNoOnclickListener(onNoOnclickListener onNoOnclickListener) { this.noOnclickListener = onNoOnclickListener; } /** * 设置确定按钮的显示内容和监听 * * @param onYesOnclickListener */ public void setYesOnclickListener(onYesOnclickListener onYesOnclickListener) { this.yesOnclickListener = onYesOnclickListener; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.datepicker_layout); //按空白处不能取消动画 setCanceledOnTouchOutside(false); //初始化界面控件 setupViews(); //操作事件 initEvent(); } private void initEvent() { tv_date_cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (noOnclickListener != null) { noOnclickListener.onNoClick(); } } }); tv_date_sure.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (yesOnclickListener != null) { yesOnclickListener.onYesClick(); mCallBackListener.getDate(date); } } }); } private void setupViews() { tv_date_cancel = (TextView) findViewById(R.id.tv_date_cancel); tv_date_sure = (TextView) findViewById(R.id.tv_date_sure); tv_title = (TextView) findViewById(R.id.tv_title); tv_title.setText(title); mWheelYear = (WheelView) findViewById(R.id.wv_year); mWheelMonth = (WheelView) findViewById(R.id.wv_month); mWheelDay = (WheelView) findViewById(R.id.wv_day); // 格式化当前时间,并转换为年月日整型数据 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()); String[] split = sdf.format(new Date()).split("-"); int currentYear = Integer.parseInt(split[0]); int currentMonth = Integer.parseInt(split[1]); int currentDay = Integer.parseInt(split[2]); // 设置默认年月日为当前日期 mWheelYear.setData(getYearData(currentYear)); mWheelYear.setDefault(0); mWheelMonth.setData(getMonthData()); mWheelMonth.setDefault(currentMonth - 1); mWheelDay.setData(getDayData(getLastDay(currentYear, currentMonth))); mWheelDay.setDefault(currentDay - 1); mWheelYear.setOnSelectListener(this); mWheelMonth.setOnSelectListener(this); mWheelDay.setOnSelectListener(this); } /** * 年范围在:1900~今年 * * @param currentYear * @return */ private ArrayList<String> getYearData(int currentYear) { ArrayList<String> list = new ArrayList<>(); for (int i = currentYear; i >= 1900; i--) { list.add(String.valueOf(i)); } return list; } private ArrayList<String> getMonthData() { ArrayList<String> list = new ArrayList<>(); for (int i = 1; i <= 12; i ) { list.add(String.valueOf(i)); } return list; } /** * 日范围在1~lastDay * * @param lastDay * @return */ private ArrayList<String> getDayData(int lastDay) { //ignore condition ArrayList<String> list = new ArrayList<>(); for (int i = 1; i <= lastDay; i ) { list.add(String.valueOf(i)); } return list; } /** * 判断是否闰年 * * @param year * @return */ private boolean isLeapYear(int year) { return (year % 100 == 0 && year % 400 == 0) || (year % 100 != 0 && year % 4 == 0); } /** * 获取特定年月对应的天数 * * @param year * @param month * @return */ private int getLastDay(int year, int month) { if (month == 2) { // 2月闰年的话返回29,防止28 return isLeapYear(year) ? 29 : 28; } // 一三五七八十腊,三十一天永不差 return month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 ? 31 : 30; } @Override public void endSelect(View view, int id, String text) { // 滚轮滑动停止后调用 switch (view.getId()) { case R.id.wv_year:{ getYear(); } case R.id.wv_month: { getMonth(); // 记录当前选择的天数 int selectDay = Integer.parseInt(getDay()); // 根据当前选择的年月获取对应的天数 int lastDay = getLastDay(Integer.parseInt(getYear()), Integer.parseInt(getMonth())); // 设置天数 mWheelDay.setData(getDayData(lastDay)); // 如果选中的天数大于实际天数,那么将默认天数设为实际天数;否则还是设置默认天数为选中的天数 if (selectDay > lastDay) { mWheelDay.setDefault(lastDay - 1); } else { mWheelDay.setDefault(selectDay - 1); } } case R.id.wv_day: { getDay(); } getDate(); } } @Override public void selecting(View view, int id, String text) { } /** * 设置确定按钮和取消被点击的接口 */ public interface onYesOnclickListener { public void onYesClick(); } public interface onNoOnclickListener { public void onNoClick(); } //接口回调,返回日期数据 public interface CallBackListener { public void getDate(String date); } public void setCallBackListener(CallBackListener mCallBackListener){ this.mCallBackListener = mCallBackListener; }

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复